'use strict';

var URL = require('url');

global.es = {
  source:{ ip: "172.30.1.78:9200", verison:"1.6"},
  current:{ ip: "172.30.1.78:9200", verison:"5.0"},
};

module.exports = function(server) {
  // Install a `/` route that returns server status
  var router = server.loopback.Router();
  var wdajbh = server.models.wd_AJBH;
  var wdsfz = server.models.wd_SFZ;
  var wdxm = server.models.wd_XM;


  router.get('/loopbackStatus', server.loopback.status());

  router.get('/', function(req, res) {
    res.render('default', {});
  });

  router.get('/sfzmb', function(req, res) {
    var arg = URL.parse(req.url, true).query;
    if(arg.wdid){
      wdsfz.find({"order":"id","where":{"id":arg.wdid},"include":["sfz-xxr","sfz-bar","sfz-zr","sfz-bhr","sfz-ksszyry"]},function (err,data){

        var arr=data;
        res.render('sfzmb', {
          alldata: arr
        });
      })
    }else{
      res.end("errow");
    }

  });
  router.get('/ajmb', function(req, res) {
    var arg = URL.parse(req.url, true).query;
    if(arg.wdid){
      wdajbh.find({"order":"id","where":{"id":arg.wdid},"include":["ajbh-xzaj","ajbh-xsaj","ajbh-xxr","ajbh-bar","ajbh-zr","ajbh-bhr"]},function (err,data){
        var arr=data;
        res.render('ajmb', {
          alldata: arr
        });
      })
    }else{
      res.end("errow");
    }

  });

  router.get('/allmb', function(req, res) {
    var arg = URL.parse(req.url, true).query;
    var LOOPBACKMODEL = server.models[arg.model];
    var mb={
      wd_DZ:["dz-ssaj-FADXZ","dz-szaj-FADXZ","dz-bjr-SJJZDZXZ","dz-gnryzsjl-ADDRESS","dz-shr-SJJZDZXZ","dz-zr-HJDZXZ"],
      wd_XM:["xm-xxr-XM","xm-hjxx-XM","xm-bjr-XM","xm-ksszyryjbxx-XM","xm-ksszyryjbxx-BMCH","xm-shr-XM","xm-zr-XM","xm-gnryzsjl-XM"],
      wd_HH:[["hh-hjxx-HH"]],
      wd_FWCS:["fwcs-xxr-SJJZDZXZ","fwcs-hjxx-FWCS","fwcs-bjr-HJDZXZ","fwcs-bjr-FWCS","fwcs-ksszyryjbxx-FWCS"],
      wd_AJSLWH:["ajslwh-xsaj-SLWH","ajslwh-xzaj-SLWH"],
      wd_AJMC:["ajmc-xsaj-AJMC","ajmc-xzaj-AJMC"],
      wd_AJBH:["ajbh-xzaj-AJBH","ajbh-xsaj-AJBH","ajbh-xxr-AJBH","ajbh-shr-AJBH","ajbh-bjr-AJBH","ajbh-zr-AJBH"],
      wd_DHHM:["dhhm-hjxx-LXFS","dhhm-bjr-LXFS","dhhm-shr-LXFS","dhhm-zr-LXFS"],
      wd_SFZ:["sfz-xxr-SFZH","sfz-bjr-SFZH","sfz-zr-SFZH","sfz-shr-SFZH","sfz-ksszyryjbxx-SFZH","sfzh-hjxx-SFZH","sfzh-gnryzsjl-CERTVALUE"],
      wd_JDMC:["jdmc-gnryzsjl-HOTELNAME"]
    };
    if(arg.wdid){
      LOOPBACKMODEL.find({"order":"id","where":{"id":arg.wdid},"include":mb[arg.model]},function (err,data){
        var arr=data;
        res.render('allmb', {
          alldata: arr,
          model:arg.model
        });
      })
    }else{
      res.end("errow");
    }

  });

  router.get('/xmmb', function(req, res) {
    var arg = URL.parse(req.url, true).query;
    if(arg.wdid){
      wdxm.find({"order":"id","where":{"id":arg.wdid},"include":["xm-xxr-XM","xm-hjxx-XM","xm-bjr-XM","xm-ksszyryjbxx-XM","xm-ksszyryjbxx-BMCH","xm-shr-XM","xm-zr-XM","xm-gnryzsjl-XM"]},function (err,data){

        var arr=data;
        res.render('xmmb', {
          alldata: arr
        });
      })
    }else{
      res.end("errow");
    }

  });

  router.post("/search",function (req,res) {

    wdajbh.find({"where":{"id.keyword":{"like":req.body.key}},"fields":["id"],"limit":50, "order": ["id.keyword"]},function (err,ajdata) {
      wdsfz.find({"where":{"id.keyword":{"like":req.body.key}},"fields":["id"],"limit":50, "order": ["id.keyword"]},function (err,sfzdata) {
        wdxm.find({"where":{"id.keyword":{"like":req.body.key}},"fields":["id"],"limit":50, "order": ["id.keyword"]},function (err,xmdata) {
          res.render('wdlist', {
            ajdata: ajdata,
            sfzdata: sfzdata,
            xmdata:xmdata
          });
        });
      });
    });

  })
  server.use(router);
};
